/***************************************************************************
 *   Copyright (C) 2010 by programmeboy                                    *
 *   programmeboy@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
 ***************************************************************************/
 
#ifndef _CPU__H__
#define _CPU__H__

#include <ntddk.h>
//#include "bp.h"

#define DO_NOTHING  __asm nop;
#define SET_CR3(_NewCR3, _OldCR3)      \
                __asm MOV EAX, CR3     \
                __asm MOV _OldCR3, EAX \
                __asm MOV EAX, _NewCR3 \
                __asm MOV CR3, EAX

#pragma pack(1)
typedef struct _SAVED_REGISTOR
{
    ULONG REAX;
    ULONG RECX;
    ULONG REDX;
    ULONG REBX;
    ULONG RESP;
    ULONG REBP;
    ULONG RESI;
    ULONG REDI;
    //ULONG RFlag;

}SAVED_REGISTOR, *PSAVED_REGISTOR;

typedef struct _VMX_FEATURES
{
    unsigned SSE3		:1;		// SSE3 Extensions
    unsigned RES1		:2;
    unsigned MONITOR	:1;		// MONITOR/WAIT
    unsigned DS_CPL	    :1;		// CPL qualified Debug Store
    unsigned VMX		:1;		// Virtual Machine Technology
    unsigned RES2		:1;
    unsigned EST		:1;		// Enhanced Intel?Speedstep Technology
    unsigned TM2		:1;		// Thermal monitor 2
    unsigned SSSE3	    :1;		// SSSE3 extensions
    unsigned CID		:1;		// L1 context ID
    unsigned RES3		:2;
    unsigned CX16		:1;		// CMPXCHG16B
    unsigned xTPR		:1;		// Update control
    unsigned PDCM		:1;		// Performance/Debug capability MSR
    unsigned RES4		:2;
    unsigned DCA		:1;
    unsigned RES5		:13;

} VMX_FEATURES, *PVMX_FEATURES;

typedef struct _CPU_VM_CONTEXT
{
    
    //
    //  CpuIndex means the current CPU Index
    //
    
    ULONG CpuIndex;

    //
    //  
    //
    
    PHYSICAL_ADDRESS pVMCSPyhicalAddr;
    PVOID pVMCSVirtualAddr;

    //
    //  
    //

    PHYSICAL_ADDRESS pVMONPyhicalAddr;
    PVOID pVMONVirtualAddr;
    //
    //  Host Stack
    //
    
    PVOID pHostEsp;

    //
    //  The IO Bitmap use to catch the event of keyboard
    //
    
    PHYSICAL_ADDRESS pIOBitmapPyhicalAddrA;
    PVOID pIOBitmapVirtualAddrA;
    PHYSICAL_ADDRESS pIOBitmapPyhicalAddrB;
    PVOID pIOBitmapVirtualAddrB;
    
    //
    //  Except bitmap used to catch the debug event
    //
    
    ULONG32 ExceptBitmap;
    
    //
    //  MSR bitmap used to catch the msr read/write event
    //
    
    PHYSICAL_ADDRESS pMSRBitmapPyhicalAddr;
    PVOID pMSRBitmapVirtualAddr;

}CPU_VM_CONTEXT, *PCPU_VM_CONTEXT;
#pragma pack()

BOOLEAN 
ClearWpAndGainCPU();
VOID 
SetWpAndReleaseCPU();

#endif